package com.chuanjiabao.wisdom.ui.activity;

import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;

import com.chuanjiabao.wisdom.MainActivity;
import com.chuanjiabao.wisdom.R;
import com.chuanjiabao.wisdom.databinding.ActivityAuthRenZhengBinding;
import com.chuanjiabao.wisdom.ui.BaseActivity;
import com.chuanjiabao.wisdom.util.HTTPAPI;
import com.chuanjiabao.wisdom.util.LogUtil;
import com.chuanjiabao.wisdom.util.StringUtil;
import com.chuanjiabao.wisdom.util.ToastUtil;
import com.lzy.okgo.callback.StringCallback;

import okhttp3.Call;
import okhttp3.Response;

public class AuthRenZhengActivity extends BaseActivity {

    private ActivityAuthRenZhengBinding binding;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this, R.layout.activity_auth_ren_zheng);
        binding.setActivity(this);
        initView();
    }

    private void initView() {
        binding.tvModuleOk.setOnClickListener(this);
        binding.ivModuleDelete.setOnClickListener(this);
        binding.tvModuleForget.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.ivModuleDelete:
                finish();
                break;
            case R.id.tvModuleForget:
                startActivity(new Intent(
                        this,
                        InputOldSecretPwdActivity.class
                ));
                break;
            case R.id.tvModuleOk:
                String pwd = binding.etModuleSecretPwd.getText().toString().trim();
                if (TextUtils.isEmpty(pwd)) {
                    ToastUtil.showToast("请输入您的密码");
                    return;
                }
                auth(pwd);
                break;
        }
    }

    public void auth(String pwd) {
        HTTPAPI.getInstance().authRenzheng(pwd, new StringCallback() {
            @Override
            public void onSuccess(String s, Call call, Response response) {
                LogUtil.e("私密密码认证成功" + s);
                if (!StringUtil.isFail(s)) {
                    StringUtil.showFail(AuthRenZhengActivity.this, s);
                } else {
                    Intent intent = new Intent(AuthRenZhengActivity.this, MainActivity.class);
                    intent.putExtra("module_status", "succ");
                    setResult(RESULT_OK, intent);
                    finish();
                }
            }
        });
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            moveTaskToBack(true);
            return true;//不执行父类点击事件
        }
        return super.onKeyDown(keyCode, event);//继续执行父类其他点击事件
    }

}
